package com.itheima.activities.rabbitmq;

import com.alibaba.fastjson2.JSON;
import com.itheima.activities.mapper.RedPacketListMapper;
import com.itheima.entity.pojo.RedPacketList;
import io.lettuce.core.dynamic.annotation.Key;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author :wujunjie
 * createTime :2022-12-23 20:31
 * description :
 */
@Component
@Slf4j
public class RedPacketListListener {
    @Autowired
    private RedPacketListMapper redPacketListMapper;
    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue("redPacketListUpdate.derect"),
                    exchange = @Exchange("redPacketListUpdate.direct"),
                    key = {"redPacketList"}
            )
    )
    void updateRedPacketList(String message){
        RedPacketList redPacketList = JSON.parseObject(message, RedPacketList.class);
        Boolean update=redPacketListMapper.updateById(redPacketList);
        if (!update){
            throw new RuntimeException("数据更新失败");
        }
    }
}
